package com.agenda;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BaseDatos extends SQLiteOpenHelper {
	
	private static final String DATABASE_NAME = "Agenda";
	private static final int DATABASE_VERSION = 1;
	
	//Sentencia para crear la BD
	private static final String DATABASE_USER_CREATE = 
			"CREATE TABLE usuario(login TEXT NOT NULL PRIMARY KEY, " +
								 "pass TEXT NOT NULL, " +
								 "valido TEXT NOT NULL, " +
								 "ultimo_acceso TEXT NOT NULL);";

	private static final String DATABASE_PERSON_CREATE = 
			"CREATE TABLE persona(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
								  "zona TEXT NOT NULL, " +
								  "nombre TEXT NOT NULL, " +
								  "negocio TEXT, " +
								  "telf1 TEXT, " +
								  "telf2 TEXT, " +
								  "telf3 TEXT, " +
								  "direccion TEXT, " +
								  "email TEXT, " +
								  "tarjeta TEXT, " +
								  "mapa TEXT, " +
								  "observacion TEXT);";
	
	
	public BaseDatos(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		//SQLiteDatabase.openOrCreateDatabase("/mnt/sdcard/data/Agenda/db.db", null);
	}

	//Este metodo es llamado cuando se crea la BD
	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_USER_CREATE);
		database.execSQL(DATABASE_PERSON_CREATE);
	}

	//Metodo que se llama cada vez que se actualiza la BD
	//Incrementa la version
	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
			Log.w(BaseDatos.class.getName(),
					"Upgrading database from version " + oldVersion + " to "
							+ newVersion + ", which will destroy all old data");
			database.execSQL("DROP TABLE IF EXISTS usuario");
			database.execSQL("DROP TABLE IF EXISTS persona");
			onCreate(database);
		}
	

}
